<?php

class Profile_Controller extends Public_Controller
{
    /**
     * 会员列表
     */
    public function indexAction()
    {
        $page_id = (int)$this->get('page');
		$page_id = ($page_id == 0) ? 1 : $page_id;

        //审核用户
        $user_ids = $this->post('user_id');
        if(is_array($user_ids) && sizeof($user_ids))
        {
            $lock = (safe::get('lock') == 'true') ? 1 : 0;
            $update = array('audit' => $lock);
            foreach($user_ids as $k => $v)
            {
                $this->model('users')->update($update, 'user_id = '.$v);
            }
        }

        //读取配置信息
        $case = $this->load_config('case');

        //获取登录信息
        $userinfo = acl::profile();
        $user_id = $userinfo['user_id'];
        
        //主要数据模型
        $data = $this->model('users')->from('users')->where('type=2')->order('user_id DESC');

        //审核标识
        $audit = $this->get('audit');
        $select['audit'] = $audit;
        //筛选组合
        if(is_numeric($audit))
        {
            $data->where('audit=?',$audit);
        }
        //获取主数据
        $data = $data->page_limit($page_id, $case['page_sum'])->select();

        //统计数据库总条数
        $total = $this->model('users')->count('users', 'user_id', 'type=2');

        $pager = new pager();
        $pager->conf($total, $page_id, $this->get_action_url().'?page', $case['page_sum'], $case['page_few']);
        $pagelist = $pager->getlink();

        $this->view->set(array(
            'data' => $data,
            'select' => $select,
            'page_id'   => $page_id,
            'pagelist_html' => $pagelist,
        ));
        $this->view->display();
    }

    /**
     * 管理员添加帐户
     */
    public function addAction()
    {
        //插入新数据和更新表
        if($info = $this->post('info'))
        {
            //写入数据库
            $users_model = $this->model('users');

            if(empty($info['username']))
                $this->alert('对不起，登录名称必须填写', -1);

            if(empty($info['nickname']))
                $this->alert('对不起，姓名没有填写', -1);

            if(empty($info['password']))
                $this->alert('对不起，密码必须填写', -1);

            if(empty($info['rpassword']))
                $this->alert('对不起，确认密码必须填写', -1);

            if(trim($info['rpassword']) != trim($info['password']))
                $this->alert('对不起，密码和确认密码不匹配', -1);

            if(empty($info['address']))
                $this->alert('对不起，联系地址必须填写', -1);
            
            $info['password'] = md5($info['password']);
            $info['created'] = date('Y-m-d H:i:s');
            $info['type'] = 2;
            $info['audit'] = 1;

            $res = $users_model->insert($info);
            $nickname = $info['nickname'];
            unset($info);

            if($res)
            {
                //写入角色数据
                $user_id = $users_model->get_insert_id();
                $users_model->query("INSERT INTO `acl_member` (`user_id`, `role_id` ,`desc`) VALUES ('$user_id', '2', '$nickname') ");
            }

            $msg = $res ? '恭喜你，用户添加成功！' : '用户添加失败，请联系管理员！';
            $this->alert($msg, $this->view->_data['base_url'].'profile/index');
        }
        $this->view->display();
    }
    
    //管理员修改帐户信息
    public function modifyAction()
    {
        //实例用户模型
        $user_model = $this->model('users');
        if(safe::get('user_id') > 0)
            $user_id = (int)safe::get('user_id');
        elseif($this->post('user_id') > 0)
            $user_id = (int)$this->post('user_id');

        if($user_id <= 0)
            $this->alert('对不起，会员账户信息错误', -1);

        $data = $user_model->find($user_id);

        //插入新数据和更新表
        if($info = $this->post())
        {
            if($info['username'] != $data['username'])
                $this->alert('对不起，非法提交信息', -1);

            if(empty($info['nickname']))
                $this->alert('对不起，姓名没有填写', -1);

            if(empty($info['address']))
                $this->alert('对不起，联系地址必须填写', -1);
            
            if(strlen($info['password']) > 0 && strlen($info['password']) < 6)
                $this->alert('对不起，密码不能小于6位数', -1);

            if(strlen($info['password']) >= 6)
                $info['password'] = md5($info['password']);
            else
                unset($info['password']);

            $res = $user_model->update($info, 'user_id = '.$user_id);

            $msg = $res ? '用户信息修改成功！' : '用户修改失败，请联系管理员！';
            $this->alert($msg, $this->view->_data['base_url'].'profile/index');
        }
        $this->view->set(array(
            'data' => $data
        ));
        $this->view->display();
    }

    /**
     * 修改资料
     */
    public function editAction()
    {
        //实例用户模型
        $user_model = $this->model('users');
        
        //获取登录信息
        $userinfo = acl::profile();
        $user_id = $userinfo['user_id'];
        
        if($user_id <= 0)
            $this->alert('对不起，账户信息错误', -1);

        $data = $user_model->find($user_id);

        //插入新数据和更新表
        if($info = $this->post())
        {
            if($info['username'] != $data['username'])
                $this->alert('对不起，非法提交信息', -1);

            if(empty($info['nickname']))
                $this->alert('对不起，姓名没有填写', -1);

            if(empty($info['address']))
                $this->alert('对不起，联系地址必须填写', -1);

            $res = $user_model->update($info, 'user_id = '.$user_id);

            $msg = $res ? '用户信息修改成功！' : '用户修改失败，请联系管理员！';
            $this->alert($msg, $this->view->_data['base_url'].'profile/edit');
        }

        $this->view->set(array(
            'data' => $data
        ));
        $this->view->display();
    }

    /**
     * 账户注册
     */
    public function registerAction()
    {
        //插入新数据和更新表
        if($post = $this->post())
        {
            //写入数据库
            $users_model = $this->model('users');
            $info = $post['info'];

            if(empty($info['username']))
                $this->alert('对不起，登录名称必须填写', -1);

            if(empty($info['nickname']))
                $this->alert('对不起，姓名没有填写', -1);

            if(empty($info['password']))
                $this->alert('对不起，密码必须填写', -1);

            if(empty($info['rpassword']))
                $this->alert('对不起，确认密码必须填写', -1);

            if(trim($info['rpassword']) != trim($info['password']))
                $this->alert('对不起，密码和确认密码不匹配', -1);

            if(empty($info['address']))
                $this->alert('对不起，联系地址必须填写', -1);
            
            $info['password'] = md5($info['password']);
            $info['created'] = date('Y-m-d H:i:s');
            $info['type'] = 2;

            $res = $users_model->insert($info);
            $nickname = $info['nickname'];
            unset($info);

            if($res)
            {
                //写入角色数据
                $user_id = $users_model->get_insert_id();
                $users_model->query("INSERT INTO `acl_member` (`user_id`, `role_id` ,`desc`) VALUES ('$user_id', '2', '$nickname') ");
                //写入浏览器登录记录
                $userinfo = array('user_id' => $user_id,'role_id' => 2);
                cookie::set('login', $userinfo, 3600*6);
            }

            $msg = $res ? '用户注册成功，请按首页指导信息进行其他操作！' : '用户注册失败，请联系管理员！';
            $this->alert($msg, $this->view->_data['root_url'],'_top');
        }
        $this->view->display();
    }
    
    // ajax方式调用查询
    public function ajax_user_fieldAction()
    {
         $user_model = $this->model('users');
         if($get = safe::get())
         {
            $key = $get['clientid'];
            $res = $user_model->getOne($key.'= ?', $get[$key]);
            if(is_array($res))
            {
                //检查修改的字段是不是自己
                if($get['user_id'] > 0)
                    $userinfo = $get;
                else
                    $userinfo = acl::profile();

                if($res['user_id'] == $userinfo['user_id'])
                {
                    exit('1');
                }
                exit('0');
            } 
            else 
            {
                exit('1');
            }
         }
    }

    /**
     * 验证码方法
     */
    public function pincodeAction()
    {
        captcha::singleton()->show();
    }

    /**
     * 修改密码
     */
    public function loginAction()
    {
        //插入新数据和更新表
        if($post = $this->post())
        {
            //开启回话
            session_start();

            //模型实例
            $user_model = $this->model('users');
            //查询用户信息
            $user_data = $user_model->getOne('username=?', $post['username']);

            //分析检验码
            if(!captcha::singleton()->check($post['pincode']))
                $this->alert('对不起，验证码错误', -1);

            if(!$user_data)
                $this->alert('对不起，用户名或者密码错误', -1);

            //检查锁定
            if($user_data['audit'] == 0)
                $this->alert('对不起，帐户被锁定，请联系管理员', -1);
                
            if($user_data['password'] != md5($post['password']))
                $this->alert('对不起，用户名或者密码错误', -1);

            $userinfo = acl::profile($user_data['user_id']);
            $_cookie = array('user_id' => $userinfo['user_id'], 'role_id' => $userinfo['role_id']);
            cookie::set('login', $_cookie, 3600*6);

            $this->alert('恭喜你，登录成功！', $this->view->_data['base_url'], '_top');
        }
        //显示登录模板文件
        $this->view->display();
    }

    /**
     * 修改密码
     */
    public function passwordAction()
    {
        //实例用户模型
        $user_model = $this->model('users');
        
        //获取登录信息
        $userinfo = acl::profile();
        $user_id = $userinfo['user_id'];
        $userdata = $user_model->find($user_id);

        if($user_id <= 0)
            $this->alert('对不起，账户信息错误', -1);

        //插入新数据和更新表
        if($post = $this->post())
        {
            $info = $post['info'];

            if(empty($info['original_password']))
                $this->alert('对不起，原始密码必须填写', -1);

            $original_password = trim($info['original_password']);
            $res = $user_model->getOne('password=?', md5($original_password));

            if(!is_array($res))
                $this->alert('对不起，原始密码不正确', -1);

            if(empty($info['password']))
                $this->alert('对不起，密码必须填写', -1);

            if(empty($info['rpassword']))
                $this->alert('对不起，确认密码必须填写', -1);

            if(trim($info['rpassword']) != trim($info['password']))
                $this->alert('对不起，密码和确认密码不匹配', -1);
            
            $update['password'] = md5(trim($info['password']));
            $res = $user_model->update($update, 'user_id = '.$user_id);

            $msg = $res ? '密码修改成功，下次登录系统请使用新密码！' : '密码修改失败，请联系管理员！';
            $this->alert($msg, $this->view->_data['base_url'].'index/right');
        }
        $this->view->set(array(
            'userdata' => $userdata
        ));
        $this->view->display();
    }

    /**
     * 修改密码
     */
    public function logoutAction()
    {
        cookie::delete('case_login');
        $this->alert('注销成功，马上为你转到首页！', $this->view->_data['root_url'], '_top');
    }

    /**
     * 删除用户
     */
    public function deleteAction()
    {
        $user_id = (int)safe::get('user_id');
        $user_ids = (array)$this->post('user_id');
        $user_ids[] = $user_id;
        foreach($user_ids as $v)
        {
            if($v > 0)
            {
                $this->model('users')->delete('user_id=?', $v);
                $this->model('datas')->delete('user_id=?', $v);
                $this->model('users')->query("DELETE FROM `acl_member` WHERE `user_id` = {$v}");
            }
        }
        $this->alert('恭喜你，用户和病例数据删除成功', $this->view->_data['base_url'].'profile/index');
    }
}
